package com.mark.springsecurity.dto.req;

import com.mark.springsecurity.domain.SysRole;
import com.mark.springsecurity.dto.req.vo.SysAuthorityVo;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

@Data
public class SysRoleCmd {
    @NotBlank
    private String name;

    private Set<SysAuthorityVo> authorities = Collections.emptySet();

    public static SysRole buildSysRole(SysRoleCmd sysRoleCmd) {
        return SysRole.builder()
                .name(sysRoleCmd.getName())
                .authorities(
                        sysRoleCmd.getAuthorities().stream()
                                .map(SysAuthorityVo::buildAuthority)
                                .collect(Collectors.toSet())
                )
                .enabled(Boolean.TRUE)
                .build();
    }

}
